//
//  MainViewController.m
//  A段项目音乐播放
//
//  Created by dllo on 15/5/21.
//  Copyright (c) 2015年 lanou3g.com蓝鸥科技. All rights reserved.
//

#import "MainViewController.h"
#import "firsPageViewController.h"
#import "rankingViewController.h"
#import "loginViewController.h"
#import "singerViewController.h"
#import "ClassViewController.h"
#import <AFNetworking/AFNetworking.h>
#import "SearchViewController.h"
#import "MusicListViewController.h"
#import "CollectListViewController.h"
#import "rankListController.h"
#import "rankingViewController.h"
#import "ClassSongConntroller.h"

@interface MainViewController ()
@property (nonatomic ,retain) UIBarButtonItem *button;
@end

@implementation MainViewController

@synthesize leftSwipeGestureRecognizer ,rightSwipeGestureRecognizer;

-(void)dealloc
{

    [leftSwipeGestureRecognizer release];
    [rightSwipeGestureRecognizer release];

    
    

    [_menuColor release];
    [_button release];
    [super dealloc];

}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    
    self =[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        
    }
    return self;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSArray *imageList = @[[UIImage imageNamed:@"首页.png"],[UIImage imageNamed:@"榜单.png"], [UIImage imageNamed:@"音乐账号.png"], [UIImage imageNamed:@"歌手.png"], [UIImage imageNamed:@"歌单.png"]];
    
    
//    self.navigationController.navigationItem.title = @"Miu";
    self.navigationItem.title = @"Miu";
    
    [self initWithImages:imageList Button:nil];
    
    [self insertMenuButtonOnView:self.view atPosition:CGPointMake(self.view.frame.size.width-100 , 50)];
   
    self.navigationController.navigationBar.translucent=NO;
   
    self.view.backgroundColor =[UIColor cyanColor];
    
    self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
    self.rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
    self.leftSwipeGestureRecognizer.direction =  UISwipeGestureRecognizerDirectionLeft;
    self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:self.leftSwipeGestureRecognizer];
    [self.view addGestureRecognizer:self.rightSwipeGestureRecognizer];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchBarAction:)];
    
    [self createButton];
    
    [self builtInterface];
}

- (void)createButton
{
    UIButton *buttonList = [[UIButton alloc]initWithFrame:CGRectMake(5, 5, self.view.frame.size.width / 3 * 2 - 5, ((self.view.frame.size.height - 70) / 2) - 5)];
    UIImage *imageList = [UIImage imageNamed:@"63.jpg"];
    [buttonList setImage:imageList forState:UIControlStateNormal];
    [buttonList setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [buttonList setBackgroundColor:[UIColor redColor]];
//    [buttonList setTitle:@"我的歌单" forState:UIControlStateNormal];
    [buttonList addTarget:self action:@selector(buttonList:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonList];
    
    
    UIButton *buttonCollect = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 3 * 2 + 5 , 5, self.view.frame.size.width / 3 - 10, ((self.view.frame.size.height - 70) / 4) - 5)];
    UIImage *imageCollect = [UIImage imageNamed:@"70.jpg"];
    [buttonCollect setImage:imageCollect forState:UIControlStateNormal];
//    [buttonCollect setTitle:@"收藏列表" forState:UIControlStateNormal];
    [buttonCollect setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [buttonCollect setBackgroundColor:[UIColor greenColor]];
    [buttonCollect addTarget:self action:@selector(buttonCollect:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonCollect];
    
    
    UIButton *buttona = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 3 * 2 + 5, (self.view.frame.size.height - 70) / 4 + 5 , self.view.frame.size.width / 3 - 10, ((self.view.frame.size.height - 70) / 4) - 5)];
    UIImage *imagea = [UIImage imageNamed:@"66.jpg"];
    [buttona setImage:imagea forState:UIControlStateNormal];
//    [buttona setBackgroundColor:[UIColor blueColor]];
    [buttona addTarget:self action:@selector(buttona:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttona];
    
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(5, (self.view.frame.size.height - 70) / 2 + 5 , self.view.frame.size.width / 3 - 5 , (self.view.frame.size.height - 70) / 4 - 5)];
    UIImage *image = [UIImage imageNamed:@"67.jpg"];
    [button setImage:image forState:UIControlStateNormal];
    [button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
//    [button setBackgroundColor:[UIColor grayColor]];
    
    
    UIButton *buttonb = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 3 + 5, (self.view.frame.size.height - 70) / 2 + 5, self.view.frame.size.width / 3 * 2 - 10, ((self.view.frame.size.height - 70) / 4) - 5)];
    UIImage *imageb = [UIImage imageNamed:@"68.jpg"];
    [buttonb setImage:imageb forState:UIControlStateNormal];
    [buttonb addTarget:self action:@selector(buttonb:) forControlEvents:UIControlEventTouchUpInside];
//    [buttonb setBackgroundColor:[UIColor blackColor]];
    [self.view addSubview:buttonb];
    
    
    UIButton *buttonLocal = [[UIButton alloc]initWithFrame:CGRectMake(5, (self.view.frame.size.height - 70) / 4 * 3 + 5, self.view.frame.size.width - 10, (self.view.frame.size.height - 70) / 6 - 10)];
    UIImage *imageL = [UIImage imageNamed:@"65.jpg"];
    [buttonLocal setImage:imageL forState:UIControlStateNormal];
//    [buttonLocal setTitle:@"本地音乐" forState:UIControlStateNormal];
    [buttonLocal setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [buttonLocal addTarget:self action:@selector(buttonLocal:) forControlEvents:UIControlEventTouchUpInside];
//    [buttonLocal setBackgroundColor:[UIColor orangeColor]];
    [self.view addSubview:buttonLocal];
    
    
    [self.view sendSubviewToBack:button];
    [self.view sendSubviewToBack:buttonb];
    [self.view sendSubviewToBack:buttonCollect];
    [self.view sendSubviewToBack:buttonList];
    
}


- (void)buttonLocal:(UIButton *)sender
{
    
}


- (void)button:(UIButton *)sender
{
    singerViewController *singer = [[singerViewController alloc]init];
    [self.navigationController pushViewController:singer animated:YES];
}


- (void)buttona:(UIButton *)sender
{
    rankingViewController *ranking = [[rankingViewController alloc]init];
    [self.navigationController pushViewController:ranking animated:YES];
}


- (void)buttonb:(UIButton *)sender
{
    ClassViewController *class = [[ClassViewController alloc]init];
    [self.navigationController pushViewController:class animated:YES];
}


- (void)buttonCollect:(UIButton *)sender
{
    CollectListViewController *collect = [[CollectListViewController alloc]init];
    [self.navigationController pushViewController:collect animated:YES];
}


- (void)buttonList:(UIButton *)sender
{
    MusicListViewController *musicList = [[MusicListViewController alloc]init];
    [self.navigationController pushViewController:musicList animated:YES];
    
}


- (void)builtInterface
{
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//    [button setBackgroundColor:[UIColor colorWithRed:0.3 green:1 blue:1 alpha:1]];
//    button.frame = CGRectMake(100, 0, 100, 30);
//    [button addTarget:self action:@selector(buttonaction) forControlEvents:UIControlEventTouchUpInside];
//    
//    [self.view addSubview:button];
//    
//    
//    UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeSystem];
//    [mybutton setBackgroundColor:[UIColor colorWithRed:0.3 green:1 blue:0.2 alpha:1]];
//    mybutton.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width / 3 * 2, [UIScreen mainScreen].bounds.size.width / 3 * 2);
//    [mybutton addTarget:self action:@selector(buttonaction) forControlEvents:UIControlEventTouchUpInside];
//    
//    [self.view addSubview:mybutton];
    
    
}
- (void)buttonaction
{
    
}
- (void)searchBarAction:(id)sender
{
    SearchViewController *search = [[[SearchViewController alloc]init] autorelease];
//    UINavigationController *search1 = [[UINavigationController alloc]initWithRootViewController:search];
    [self.navigationController pushViewController:search animated:YES];
    
}
- (void)handleSwipes:(UISwipeGestureRecognizer *)sender
{
    /* 设置翻页动画 */
   
    if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
{
         firsPageViewController *firs =[[firsPageViewController alloc]init];
        [self.navigationController pushViewController:firs animated:YES];
        CATransition *tansition =[CATransition animation];
        tansition.type =@"push";
        tansition.duration=0.8f;
//        tansition.type =@"fromRight";
        tansition.type=@"cube";
        [self.navigationController.view.layer addAnimation:tansition forKey:nil];
        [firs release ];
    }

    if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
        /* 设置翻页动画 */
        loginViewController *login =[[loginViewController alloc]init];
        CATransition *trans = [CATransition animation];
        [self.navigationController pushViewController:login animated:NO];
        trans.type = @"push";
        trans.duration = 0.8f;
        trans.subtype = @"fromLeft";
        trans.type=@"oglFlip";
        [self.navigationController.view.layer addAnimation:trans forKey:nil];
        [login release];
    }

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}

- (void)initWithImages:(NSArray*)buttonList Button:(UIBarButtonItem *)button
{
    self.button =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"设置.png"] style:UIBarButtonItemStylePlain target:self action:@selector(showMenu)];
    
    self.navigationItem.leftBarButtonItem =self.button;
    
    _backgroundMenuView = [[UIView alloc] init];
    _menuColor = [UIColor orangeColor];
    
    _buttonList = [[NSMutableArray alloc] initWithCapacity:buttonList.count];
    
    int index =1;
    
    for (UIImage *image in [buttonList copy])
    
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:image forState:UIControlStateNormal];
        
        button.frame = CGRectMake(20,(70* index),30, 50);
        
        button.tag = index + 100;
        
        [button addTarget:self action:@selector(onMenuButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [_buttonList addObject:button];
        
        ++index;
        
    }
    
}

- (void)insertMenuButtonOnView:(UIView*)view atPosition:(CGPoint)position
{
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissMenu)];
    
    UIImage *image =[UIImage imageNamed:@"menuIcon.png"];
    
    UIImageView *imageview =[[UIImageView alloc]init];
    
    [imageview setImage:image];
    
    [imageview addGestureRecognizer:singleTap];
    
    for (UIButton *button in _buttonList)
    {
        [_backgroundMenuView addSubview:button];
    }
    _backgroundMenuView.frame = CGRectMake(-90,0, 70, view.frame.size.height);
   
    _backgroundMenuView.backgroundColor = [UIColor whiteColor];
    
    _backgroundMenuView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.5f];
    
    [view addSubview:_backgroundMenuView];
    
}

- (void)dismissMenu

{
    if (_isOpen)
   
    {
        _isOpen =!_isOpen;
        
        [self performDismissAnimation];
    }
}

- (void)showMenu
{
    if (!_isOpen)
    {
        _isOpen = !_isOpen;
        
        [self performSelectorInBackground:@selector(performOpenAnimation) withObject:nil];
    }
    
    else
    {
        
        [self dismissMenu];
        
    }
    
}

- (void)onMenuButtonClick:(UIButton*)button
{
    if ([self respondsToSelector:@selector(menuButtonClicked:)])
        
        [self menuButtonClicked:button.tag];
    
    //[self dismissMenuWithSelection:button];
}

- (void)performDismissAnimation
{
    [UIView animateWithDuration:0.5 animations:^{
        _backgroundMenuView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity,0, 0);
    }];
    
}

- (void)performOpenAnimation
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.4 animations:^{
            _backgroundMenuView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity,90, 0);
        }];
    });
    for (UIButton *button in _buttonList)
        
    {
        [NSThread sleepForTimeInterval:0.02f];
        dispatch_async(dispatch_get_main_queue(), ^{
            button.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 20, 0);
            [UIView animateWithDuration:0.3f
                                  delay:0.3f
                 usingSpringWithDamping:.3f
                  initialSpringVelocity:1.f
                                options:1 animations:^{
                                    button.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, 0);
                                }
                             completion:^(BOOL finished)
             {
             }];
        });
    }
}
- (void)menuButtonClicked:(int)index
{
    
    if (index ==101) {
        firsPageViewController *firs =[[firsPageViewController alloc]init];
        
        [self.navigationController pushViewController:firs animated:YES];
        
        [firs release];
        
    }
    
    if (index==102) {
        
        rankingViewController *ranking =[[rankingViewController alloc]init];
        
        [self.navigationController pushViewController:ranking animated:YES];
        
        [ranking release];
        
    }
    
    if (index==103) {
        
        loginViewController *login =[[loginViewController alloc]init];
        
        [self.navigationController pushViewController:login animated:YES];
        
        [login release];
        
    }
    
    if (index==104) {
        singerViewController *singer =[[singerViewController alloc]init];
        
        [self.navigationController pushViewController:singer animated:YES];
        
        [singer release];
    }
    
    if (index==105) {
        
        ClassViewController *song =[[ClassViewController alloc]init];
        
        [self.navigationController pushViewController:song animated:YES];
        
        [song release];
        
    }
    
}






@end
